package com.woniuxy.yuekeuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.controller.Info.CampsiteCommentInfo;
import com.woniuxy.yuekeuser.controller.Info.CommentAndImage;
import com.woniuxy.yuekeuser.info.FileItem;
import com.woniuxy.yuekeuser.info.UserInfo;
import com.woniuxy.yuekeuser.model.CampsiteComment;
import com.woniuxy.yuekeuser.dao.CampsiteCommentMapper;
import com.woniuxy.yuekeuser.model.User;
import com.woniuxy.yuekeuser.service.ICampsiteCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@Service
public class CampsiteCommentServiceImpl extends ServiceImpl<CampsiteCommentMapper, CampsiteComment> implements ICampsiteCommentService {

    @Autowired
    private CampsiteCommentMapper campsiteCommentMapper;

    @Override
    public String campsiteComment(CampsiteCommentInfo campsiteCommentInfo) {

        //不带图片的评论
        if(campsiteCommentInfo.getFileItems().size()==0){

            int row = campsiteCommentMapper.campsiteComment(campsiteCommentInfo);

            return row!=0?"评论成功！":"评论失败！";
        }else {

            int row = campsiteCommentMapper.insertComment(campsiteCommentInfo);
            List<FileItem> fileItems = campsiteCommentInfo.getFileItems();
            for (FileItem fileItem : fileItems) {
                campsiteCommentMapper.insertCommentImage(fileItem.getUrl(),campsiteCommentInfo.getCommentId());
            }

            return  row!=0?"评论成功！":"评论失败！";
        }

    }

    @Override
    public IPage<CommentAndImage> loadCampsiteCommentById(Page<CampsiteComment> page, int campsiteId) {

        IPage<CommentAndImage> campsiteCommentIPage = campsiteCommentMapper.loadCampsiteCommentById(page, campsiteId);

        List<CommentAndImage> records = campsiteCommentIPage.getRecords();

        for (CommentAndImage record : campsiteCommentIPage.getRecords()) {


            List<String> commentImage = campsiteCommentMapper.commentImage(record.getCampsiteCommentId());

            record.setName(campsiteCommentMapper.selectNameById(record.getUserId()));

            record.setCampsiteCommentImages(commentImage);
        }

        return campsiteCommentIPage.setRecords(records);
    }
}
